home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / PlayRiffs-c / PlayRiffs.µ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  10.9 KB  |  540 lines  |  [TEXT/MMCC]

  1. #include "Sound.h"
  2.  
  3. #define over qd.screenBits.bounds.right
  4. #define down qd.screenBits.bounds.bottom
  5.  
  6. PicHandle        aboutPict, pattyPict, bluePict;
  7. PixPatHandle    Miss_Patty;
  8.  
  9. short riff;
  10.  
  11. Handle            riff_1, riff_2, riff_3, riff_4, riff_5, 
  12.                 riff_6, riff_7, riff_8, riff_9;
  13.                 
  14. SndChannelPtr    chanPtr = 0L, riff_ptr = 0L, channelPtr;
  15. SndCommand        call_back, flush_out, quiet_down, buffer_it;
  16. Boolean         Jimi_Hendrix = false, hotLicks = false, 
  17.                 quiet = false, tunes = true;
  18.  
  19. char            var_1, var_2;
  20.  
  21. WindowPtr        shell_window, splash_window;
  22.  
  23. RgnHandle        mBarRgn, GrayRgn;
  24. short            *mBarHeightPtr;
  25. short            twenty_pixels;
  26. short             riffRes;
  27. Rect             splashBounds, pictRect;
  28. long             ticks;
  29.  
  30. //• Prototypes:
  31. //• Set up...
  32. void Hide_Menu_Bar (void);
  33. void Show_Menu_Bar (void);
  34. void Do_The_Splash (void);
  35. void Create_Our_Screen (void);
  36. Boolean We_Be_Hued (void);
  37. void Wake_The_Managers (void);
  38. void Bag_Some_Riffs (void);
  39. void Setup_Snd_Commands (void);
  40.  
  41. //• ...and run.
  42. void Do_The_Sound (short whichID, Boolean asynch);
  43. void Stop_The_Riffs (void);
  44. void Jam_Session (void);
  45. pascal void Auto_Redial (SndChannelPtr chan, SndCommand order);
  46. void Play_The_Riffs (void);
  47. void Main_Event_Loop (void);
  48. void Handle_Key_Hits (EventRecord *theEvent);
  49. void Put_Things_Back (void);
  50. void main (void);
  51.  
  52. //• --------------------------------------------------------------- •//
  53. //• ----------------- S E T U P - S E C T I O N ------------------- •//
  54. //• --------------------------------------------------------------- •//
  55.  
  56. void Hide_Menu_Bar (void) 
  57. {
  58.     Rect    mBarRect;
  59.  
  60.     GrayRgn = GetGrayRgn ();
  61.     mBarHeightPtr = (short *)  0x0BAA;
  62.     twenty_pixels = *mBarHeightPtr;
  63.     *mBarHeightPtr = 0;
  64.     mBarRect = qd.screenBits.bounds;
  65.     mBarRect.bottom = mBarRect.top + twenty_pixels;
  66.     mBarRgn = NewRgn ();
  67.     RectRgn (mBarRgn, &mBarRect);
  68.     UnionRgn (GrayRgn, mBarRgn, GrayRgn);
  69.     PaintOne (0L, mBarRgn);
  70. }
  71.  
  72. void Show_Menu_Bar (void) 
  73. {
  74.     *mBarHeightPtr = twenty_pixels;
  75.     DiffRgn (GrayRgn, mBarRgn, GrayRgn);
  76.     DisposeRgn (mBarRgn);
  77. }
  78.  
  79. void Do_The_Splash ()
  80. {
  81.     
  82.     //• A centered, 300w, 250h window.
  83.     SetRect (&splashBounds, over / 2 - 150, 
  84.                             down / 2 - 125, 
  85.                             over / 2 + 150,
  86.                             down / 2 + 125);
  87.  
  88.     //• "C" window so our 'ppat" will draw.
  89.     splash_window = NewCWindow (0L,             //• No storage - LIVE!
  90.                                &splashBounds,     //• As delineated above.
  91.                                "\p",             //• No place for one.
  92.                                true,             //• Yes, it's visible
  93.                                dBoxProc,         //• For dialog border.
  94.                               (WindowPtr) -1L,     //• In front.
  95.                                false,             //• No go away.
  96.                                0);                //• Reference value 0.
  97.                               
  98.     SetPort(splash_window);
  99.  
  100.     //• splash_window is our set port, so we go off local coords.
  101.     SetRect (&pictRect, 0, 0, 300, 249);
  102.     
  103.     aboutPict = GetPicture (128);
  104.     
  105.     DrawPicture (aboutPict, &pictRect);
  106.     Delay (360, &ticks);
  107.     HideWindow (splash_window);
  108. }
  109.  
  110. void Create_Our_Screen ()
  111. {
  112.     Rect windowBounds, blueRect, sisterRect;
  113.     
  114.     windowBounds = qd.screenBits.bounds;
  115.     
  116.     shell_window = NewCWindow(0L, &windowBounds, "\p", true, plainDBox, (WindowPtr) -1L, true, 0);
  117.     SetPort(shell_window);
  118.  
  119.     Miss_Patty = GetPixPat (128);
  120.  
  121.     FillCRect (&windowBounds, Miss_Patty);
  122.  
  123.     SetRect (&blueRect, 73, 217, 73 + 38, 217 + 55);
  124.     bluePict = GetPicture (130);
  125.     DrawPicture (bluePict, &blueRect);
  126. }
  127.  
  128. Boolean We_Be_Hued ()
  129. {
  130.     SysEnvRec whichMac;
  131.     SysEnvirons (2, &whichMac);
  132.     return (whichMac.hasColorQD);
  133. }
  134.  
  135. void Wake_The_Managers ()
  136. {
  137.     MaxApplZone ();
  138.     InitGraf (&qd.thePort);
  139.     InitFonts ();
  140.     FlushEvents (everyEvent, 0);
  141.     InitWindows ();
  142.     InitMenus ();
  143.     TEInit ();
  144.     InitDialogs (0L);
  145.     InitCursor ();
  146.     
  147.     MoreMasters ();
  148.     MoreMasters ();
  149.     MoreMasters ();
  150. }
  151.  
  152. void Bag_Some_Riffs ()
  153. {        
  154. //     riffRes = OpenResFile("\pRiffs");   // Tired this - stack ate heap.
  155.                                         // It needs more work.
  156. //     if (riffRes == 0L)
  157. //         ExitToShell ();
  158.     
  159.     //• We're gathering by name.
  160.     riff_1 = GetNamedResource ('snd ', "\pRiff_01");
  161.     riff_2 = GetNamedResource ('snd ', "\pRiff_02");
  162.     riff_3 = GetNamedResource ('snd ', "\pRiff_03");
  163.     riff_4 = GetNamedResource ('snd ', "\pRiff_04");
  164.     riff_5 = GetNamedResource ('snd ', "\pRiff_05");
  165.     riff_6 = GetNamedResource ('snd ', "\pRiff_06");
  166.     riff_7 = GetNamedResource ('snd ', "\pRiff_07");
  167.     riff_8 = GetNamedResource ('snd ', "\pRiff_08");
  168.     riff_9 = GetNamedResource ('snd ', "\pRiff_09");
  169. }
  170.  
  171. void Setup_Snd_Commands ()
  172. {
  173.     chanPtr = (SndChannelPtr) NewPtrClear (sizeof (SndChannel));
  174.     if (chanPtr != 0L)
  175.     {
  176.         chanPtr->qLength = stdQLength;
  177.     }
  178.     riff_ptr = 0L;
  179.     call_back.cmd = callBackCmd;
  180.     call_back.param1 = 0;
  181.     call_back.param2 = SetCurrentA5 ();
  182.     
  183.     flush_out.cmd = flushCmd;
  184.     flush_out.param1 = 0;
  185.     flush_out.param2 = 0L;
  186.     
  187.     quiet_down.cmd = quietCmd;
  188.     quiet_down.param1 = 0;
  189.     quiet_down.param2 = 0L;
  190.     
  191.     buffer_it.cmd = bufferCmd;
  192.     buffer_it.param1 = 0;
  193.     buffer_it.param2 = 0L;
  194. }
  195.  
  196. //• --------------------------------------------------------------- •//
  197. //• -------------------- R U N - S E C T I O N -------------------- •//
  198. //• --------------------------------------------------------------- •//
  199.  
  200. //• John Calhoun's sound player, from Stella Obscura (again).
  201. void Do_The_Sound (short whichID, Boolean asynch)
  202. {
  203.     Handle theSnd;
  204.     OSErr err;
  205.     Boolean soundActive;
  206.     
  207.     soundActive = true;    
  208.     
  209.     if ((soundActive))
  210.     {
  211.         theSnd = GetResource ('snd ', whichID);
  212.         
  213.         if ((theSnd != 0L) && (ResError () == noErr))
  214.         {
  215.             if ((channelPtr != 0L))
  216.             {
  217.                 err = SndDisposeChannel (channelPtr, true);
  218.                 channelPtr = 0L;
  219.             }
  220.             if ((asynch == true) && 
  221.                 (SndNewChannel 
  222.                 (&channelPtr, 0, initMono, 0L) == noErr)) 
  223.                 err = SndPlay (channelPtr, theSnd, true); 
  224.             else    
  225.                 err = SndPlay (0L, theSnd, false);
  226.         }
  227.     }
  228. }
  229.  
  230. //• This gets the ball rolling.  Auto_Redial calls Play_The_Riffs,
  231. //• as a loop (calling it back) and the ID number is switched there.
  232. void Jam_Session ()
  233. {
  234.     Boolean formost = true;
  235.     
  236.     if (! tunes)
  237.         return;
  238.         
  239.     if (Jimi_Hendrix)
  240.         return;
  241.     
  242.     //• If it's number one, it's a new sound channel.  Otherwise,
  243.     //• we just use the one we have.
  244.     if (formost)
  245.         SndNewChannel (&riff_ptr, 0, 0, 
  246.                       (SndCallBackProcPtr) &Auto_Redial);
  247.  
  248.     //• This assignment makes "riff" be riff_01, or ID #9001.
  249.     riff = 1;
  250.     Jimi_Hendrix = true;
  251.     formost = false;
  252.     
  253.     SndPlay (riff_ptr, riff_1, true);
  254.     SndDoCommand (riff_ptr, &call_back, true);
  255. }
  256.  
  257. //• Commonly used call back routine.
  258. pascal void Auto_Redial (SndChannelPtr chan, SndCommand order)
  259. {
  260.     long saveA5;
  261.     saveA5 = SetCurrentA5 ();
  262.     SetA5 (order.param2);
  263.     Play_The_Riffs ();
  264.     SetA5 (saveA5);
  265. }
  266.  
  267. //• This is called by Auto_Redial, which is called by SndNewChannel
  268. //• (in Jam_Session).
  269. //• It starts off playing Riff_01 (ID 9001), 
  270. void Play_The_Riffs ()
  271. {
  272.     Handle which_riff;
  273.     
  274.     //• Just a couple toggles to put in more control if needed.
  275.     if (! hotLicks || ! tunes)
  276.         return;    
  277.             
  278.     //• We are already playing riff_1 coming into this routine.
  279.     //• Format 1 snd resources, please.
  280.     switch (riff)
  281.     {
  282.         //• Plays 9002 twice, then bumps up the series value.
  283.         case 1:
  284.         case 2:
  285.             riff++;
  286.             which_riff = riff_2;
  287.         break;
  288.  
  289.         case 3:
  290.             riff++;
  291.             which_riff = riff_3;
  292.         break;
  293.  
  294.         case 4:
  295.             riff++;
  296.             which_riff = riff_4;
  297.         break;
  298.  
  299.         case 5:
  300.         case 6:
  301.             riff++;
  302.             which_riff = riff_2;
  303.         break;
  304.  
  305.         case 7:
  306.         case 8:
  307.             riff++;
  308.             which_riff = riff_5;
  309.         break;
  310.  
  311.         case 9:
  312.             riff++;
  313.             which_riff = riff_6;
  314.         break;
  315.  
  316.         case 10:
  317.             riff++;
  318.             which_riff = riff_7;
  319.         break;
  320.  
  321.         case 11:
  322.             riff++;
  323.             which_riff = riff_8;
  324.         break;
  325.  
  326.         //• History repeats itself!
  327.         case 12:
  328.         case 13:
  329.             riff++;
  330.             which_riff = riff_2;
  331.         break;
  332.  
  333.         case 14:
  334.             riff++;
  335.             which_riff = riff_5;
  336.         break;
  337.  
  338.         case 15:
  339.             riff++;
  340.             which_riff = riff_6;
  341.         break;
  342.  
  343.         case 16:
  344.             riff = 1;
  345.             which_riff = riff_9;
  346.         break;
  347.     }
  348.     
  349.      var_1 = *((Ptr) (((long) *which_riff) + 3));
  350.      var_2 = *((Ptr)  ((long) *which_riff) + 10 * var_1 + 1);
  351.      
  352.      buffer_it.param2 = ((long) *which_riff) + 10 * var_1 + 10 * var_2;
  353.  
  354.     SndDoCommand (riff_ptr, &buffer_it, true);
  355.     SndDoCommand (riff_ptr, &call_back, true);
  356. }
  357.  
  358. void Stop_The_Riffs ()
  359. {
  360.     //• Don't stop it if it's already stopped.
  361.     if (! Jimi_Hendrix)
  362.         return;
  363.         
  364.     //• Boolean meets Jimi, in a negative context.
  365.     Jimi_Hendrix = false;
  366.     
  367.     //• Plays a "sound of silence."
  368.     SndDoImmediate (riff_ptr, &quiet_down);
  369.     
  370.     //• You know what happens when you flush!
  371.     SndDoCommand (riff_ptr, &flush_out, false);
  372. }
  373.  
  374. void Main_Event_Loop ()
  375. {
  376.     short            ok;
  377.     EventRecord        theEvent;
  378.     short             key;
  379.     Rect             pattyRect;
  380.     
  381.     HiliteMenu (0);        //• What menu?
  382.     WaitNextEvent (everyEvent, &theEvent, 0L, 0L);
  383.  
  384.     //• Puts in "Patty with the blue dress, blue dress on..."
  385.     SetRect (&pattyRect, 109, 132, 109 + 97, 132 +72);
  386.     pattyPict = GetPicture (129);
  387.  
  388.     hotLicks = true;    
  389.     Jam_Session ();
  390.     
  391.     switch (theEvent.what)
  392.     {
  393.         //• We do this, instead of a mouse handling routine.
  394.         case mouseDown:
  395.             DrawPicture (pattyPict, &pattyRect);
  396.             Do_The_Sound (10111, true);
  397.         break;
  398.         
  399.         //• More bull.
  400.         case mouseUp:
  401.             Do_The_Sound (10110, true);
  402.             FillCRect (&pattyRect, Miss_Patty);
  403.         break;
  404.         
  405.         case keyDown:
  406.         case autoKey:
  407.             Handle_Key_Hits (&theEvent);
  408.         break;
  409.     
  410.         case keyUp:
  411.         break;
  412.     
  413.         //• We don't use this because we hog the whole screen.
  414.         case updateEvt:
  415.             //• Where'd this come from?
  416. //            BeginUpdate (shell_window);
  417. //            DrawBullseye (((WindowPeek) shell_window)->hilited);
  418. //            EndUpdate (shell_window);
  419.         break;
  420.     
  421.         //• Same here.
  422.         case activateEvt:
  423.         break;
  424.     }
  425. }
  426.  
  427. //• Key hits ala "CheeseToast" and 100 others.
  428. void Handle_Key_Hits (EventRecord *theEvent)
  429. {
  430.     Rect aboutBounds;
  431.     short    chCode;
  432.     long ticks;
  433.     
  434.     chCode = theEvent->message & charCodeMask;
  435.  
  436.     switch (chCode) 
  437.     {
  438.         case '1':  //• User hits the 1 key.
  439.                Do_The_Sound (10001, true);
  440.         break;
  441.  
  442.         case '2':
  443.                Do_The_Sound (10002, true);
  444.         break;
  445.  
  446.         case '3':
  447.                Do_The_Sound (10003, true);
  448.         break;
  449.  
  450.         case '4':
  451.                Do_The_Sound (10004, true);
  452.         break;
  453.  
  454.         case '5':
  455.                Do_The_Sound (10005, true);
  456.         break;
  457.  
  458.         case '6':
  459.                Do_The_Sound (10006, true);
  460.         break;
  461.  
  462.         case '7':
  463.                Do_The_Sound (10007, true);
  464.         break;
  465.  
  466.         case '8':
  467.                Do_The_Sound (10008, true);
  468.         break;
  469.  
  470.         case '9':
  471.                Do_The_Sound (10009, true);
  472.         break;
  473.  
  474.         case '0':
  475.                Do_The_Sound (10009, true);
  476.         break;
  477.  
  478.         case '*':
  479.             quiet = true;        //• Give the quiet command.
  480.             Stop_The_Riffs ();
  481.         break;
  482.  
  483.         case 'q':
  484.             Put_Things_Back ();
  485.         break;
  486.     }
  487. }
  488.  
  489. void Put_Things_Back ()
  490. {
  491.     //• Get rid of the key and mouse hit sound channel.
  492.     if (chanPtr != 0L)
  493.     {
  494.         SndDoImmediate (chanPtr, &flush_out);     //• flush.
  495.         SndDisposeChannel (chanPtr, true);
  496.     }
  497.     //• Get rid of the riff sound channel.
  498.     if (riff_ptr != 0L)
  499.     {
  500.         SndDoImmediate (riff_ptr, &flush_out);     //• flush.
  501.         SndDisposeChannel (riff_ptr, true);
  502.     }
  503.     //• Back to the bar (for drinks and socializing).
  504.     Show_Menu_Bar ();
  505.     ExitToShell ();        //• Back to Findersville.
  506. }
  507.  
  508. void main ()
  509. {
  510.     Rect r1;
  511.     short i;
  512.     SysEnvRec whichMac;
  513.     
  514.     Wake_The_Managers ();
  515.         
  516.     SetEventMask (everyEvent);
  517.  
  518.     Bag_Some_Riffs ();
  519.     Do_The_Splash ();
  520.  
  521.     //• We do this here because we still look like a desktop.
  522.     Hide_Menu_Bar ();
  523.  
  524.     SysEnvirons (2, &whichMac);
  525.     
  526.     //• We don't mess with no antiques!
  527.     if (whichMac.processor == env68000) 
  528.     {
  529.         tunes = false;
  530.         ExitToShell ();
  531.     }
  532.     Create_Our_Screen ();
  533.     Setup_Snd_Commands ();
  534.     
  535.     for (;;)
  536.         Main_Event_Loop ();
  537. //    CloseResFile(riffRes);  //• Remember?
  538. }
  539.  
  540.